package com.xmwzw.catpu.validator;

/**
 * Created by chenpeiqiang on 2017/11/28.
 */

public abstract class Validator {
    protected String errorMessage;

    public Validator(String _customErrorMessage) {
        errorMessage = _customErrorMessage;
    }

    public abstract boolean isValid(String value);

    public boolean hasErrorMessage() {
        return errorMessage != null;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    /**
     * 获取字符串的长度，对双字符（包括汉字）按两位计数
     *
     * @param value
     * @return
     */
    public static int getStrLength(String value) {
        int valueLength = 0;
        String chinese = "[\u0391-\uFFE5]";
        for (int i = 0; i < value.length(); i++) {
            String temp = value.substring(i, i + 1);
            if (temp.matches(chinese)) {
                valueLength += 2;
            } else {
                valueLength += 1;
            }
        }
        return valueLength;
    }
}
